<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{data.total_price}}</text>
		</view>
		<view class="pay-type-list">
			
			
			<view v-for="(item,index) in pagList" :key="index" class="type-item b-b icon-qiyezhifu" @click="changePayType(item)" v-show="data.page!='zengzhiquan'">
				<text class="icon yticon icon-qiyezhifu"></text>
				<view class="con">
					<text class="tit">{{item.name}}</text>
					<text>{{item.subtitle}}</text>
				</view>
				<label class="radio">
					<radio value="item.type" :disabled="!item.enable"  color="#fa436a" :checked='payType == 5' />
					</radio>
				</label>
			</view>
			<!-- <view class="type-item b-b icon-qiyezhifu" @click="changePayType(5)" v-show="data.page!='zengzhiquan'">
				<text class="icon yticon icon-qiyezhifu"></text>
				<view class="con">
					<text class="tit">产品券支付</text>
					<text>推荐使用支付宝支付</text>
				</view>
				<label class="radio">
					<radio value="5"  color="#fa436a" :checked='payType == 5' />
					</radio>
				</label>
			</view> -->
			<view v-for="(item,index) in pagList" class="type-item b-b" @click="changePayType(item)" v-show="data.page=='zengzhiquan'">
				<text class="icon yticon icon-alipay"></text>
				<view class="con">
					<text class="tit">{{item.name}}</text>
					<text>{{item.subtitle}}</text>
				</view>
				<label class="radio">
					<radio :value="item.type"  color="#fa436a"  />
					</radio>
				</label>
			</view>
		</view>
		
		<text class="mix-btn" @click="submit">确认支付</text>
		<paypass ref="yspay" @cancel="cancelpay" @submit="submitpay"></paypass>
	</view>
</template>

<script>
	
	//转意符换成普通字符  
	function escape2Html(str) {  
	    var arrEntities = {  
	        'lt': '<',  
	        'gt': '>',  
	        'nbsp': ' ',  
	        'amp': '&',  
	        'quot': '"'  
	    };  
	    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {  
	        return arrEntities[t];  
	    });  
	}  
	import paypass from "@/components/ys-paypass/ys-paypass.vue";
	export default {
		components:{
			paypass
		},
		data() {
			return {
				pagList:[],
				orderInfo:'',
				qiyetip:'',
				data:{},
				payType: 2,
				orderInfo: {}
			};
		},
		computed: {
		
		},
		onLoad(options) {
			this.data=JSON.parse(options.data)
			
			this.getqiyetongzhifu()
			
			
			
			if(this.data.page!='zengzhiquan'){
				if(Number(this.data.amount)){
					this.getPayInfo()
				}
				
				this.payType=5
			}else{
				this.getfuyang()
				///api/order/get_fuyang_payment_info
			}
			
			
		},

		methods: {
			getfuyang(){
				let that=this
				that.apiget('/api/order/get_fuyang_payment_info',{
					amount:Number(that.data.amount)
				},true).then(res=>{
					that.pagList=res.Data
					for(let i=0;i<that.pagList.length;i++){
						switch(that.pagList[i].name){
							case '支付宝支付':
								that.pagList[i].type=2
							break;
							case '代金券支付':
							break;
							case '产品券支付':
							that.pagList[i].type=5
							break;
							case '人工通道':
							that.pagList[i].type=4
							break;
							case '微信支付':
							that.pagList[i].type=1
							break;
						}
					}
					
				}).catch(err=>{
					console.log(err)
				})
			},
			getPayInfo(){
				let that=this
				that.apiget('/api/order/get_goods_payment_info',{
					amount:Number(that.data.amount)
				},true).then(res=>{
					that.pagList=res.Data
					
				}).catch(err=>{
					console.log(err)
				})
			},
			payzhifuquan(){
				
				
				let that=this
				that.apipost('/api/order/goods_pay_by_productcoupon',that.data,true).then(res=>{
					if(Number(this.data.amount)){
						uni.navigateTo({
						    url: `/pages/money/paysuccess?page=${that.data.page}`
						});
					}
					
				}).catch(err=>{
					console.log(err)
				})
			},
			payzhifubao(orderInfo){
				let that=this
				this.orderInfo=orderInfo
				uni.requestPayment({
					//服务提供商 通过uni.getProvider获取
					provider: 'alipay',
					//后台返回的订单数据
					orderInfo:escape2Html(orderInfo),
					//调用成功的回调
					success(res) {
						uni.navigateTo({
						    url: `/pages/money/paysuccess?page=${that.data.page}`
						});
					},
					//调用失败的回调
					fail(err) {
						that.orderInfo=JSON.stringify(err)
						console.log('fail:' + err);
					}
				})
			},
			clickzhifubao(){
				let that=this
				switch(that.data.page){
					case 'cart':
						that.apipost('/api/order/carts_pay_by_alipay',that.data,true).then(res=>{
							that.payzhifubao(res.Data)
						}).catch(err=>{
							console.log(err)
						})
					break;
					case 'shangpin':
						that.apipost('/api/order/goods_pay_by_alipay',that.data,true).then(res=>{
							that.payzhifubao(res.Data)
						}).catch(err=>{
							console.log(err)
						})
					break;
					case 'zengzhiquan':
						that.apiget('/api/order/get_stock_pay_sign_by_alipay',that.data,true).then(res=>{
							that.payzhifubao(res.Data)
						}).catch(err=>{
							console.log(err)
						})
					break;
				}
			
			},
			getqiyetongzhifu(){
				let that=this
				that.apipost('/api/home/discount_info',{
					type:that.data.type,
					amount:that.data.amount
				},true).then(res => {
					if(res.Code==10200){
						that.qiyetip=res.Data
					}
				
				}).catch(err=>{
				console.log(err)
				});
			},
			weixinzhifu(){
				let that=this
				if(this.data.type==1){
					that.confirm()
				}else{
					if(this.data.page=='cart'){
						that.apipost('/api/cart/submit',that.data,true).then(res=>{
							that.confirm(res.Data.order_id)
						}).catch(err=>{
							console.log(err)
						})
						return
					}
					that.apipost('/api/home/order_submit',that.data,true).then(res => {
						if(res.Code==10200){	
							that.confirm(res.Data.order_id)
						}
					}).catch(err=>{
					console.log(err)
					});
				}
			},
			qiyetongzhifu(){
				let that=this
				if(this.data.type==1){
					
					that.apipost('/api/home/stock_pay_by_discount',that.data,true).then(res => {
							if(res.Code==10200){
								uni.showToast({
									icon :'none',
									title: '支付成功',
									duration: 2000
								});
								uni.navigateTo({
								    url: `/pages/money/paysuccess?page=${that.data.page}`
								});
							}
					
					
					}).catch(err=>{
					console.log(err)
					});
				}else{
					if(this.data.page=='cart'){
						that.apipost('/api/cart/carts_pay_by_discount',that.data,true).then(res=>{
							if(res.Code==10200){
								uni.showToast({
									icon :'none',
									title: '支付成功',
									duration: 2000
								});
								uni.navigateTo({
								    url: `/pages/money/paysuccess?page=${that.data.page}`
								});
							}
							
						}).catch(err=>{
							console.log(err)
						})
						return
					}
					that.apipost('/api/home/goods_pay_by_discount',that.data,true).then(res => {
						if(res.Code==10200){
							uni.showToast({
								icon :'none',
								title: '支付成功',
								duration: 2000
							});
							uni.navigateTo({
							    url: `/pages/money/paysuccess?page=${that.data.page}`
							});
						}
				
				
				}).catch(err=>{
				console.log(err)
				});
			
			}
			},
			submit(){
				switch(Number(this.payType)){
					case 1:
						this.clickzhifu()
					break;
					case 2:
						this.clickzhifubao()
					break;
					case 3:
						this.$refs.yspay.show()
					break;
					case 4:
						uni.navigateTo({
							url: `/pages/rengongshenhetongdao/index?data=${JSON.stringify(this.data)}`
						});
					break;
					case 5:
						this.$refs.yspay.show()
						
					break;
					default:
					this.clickzhifu()
				}
				
				
				//this.$refs.yspay.show();
			},
			cancelpay(){
				
			},
			submitpay(ex){
				this.data.pwd=ex.value
				this.clickzhifu()
			},
			paychaxun(data){
				let that=this
				
				let orderInfo={
					provider:'wxpay',
						"appid": data.appid,
						"noncestr": data.noncestr,
						"package":  data.package,
						"partnerid": data.partnerid,
						"prepayid": data.prepayid,
						"timestamp": data.timestamp,
						"sign": data.sign
					}
					
				uni.requestPayment({
					orderInfo:orderInfo,
					provider:'wxpay',
					_debug:1,
					success (res) {
					
					  if(res.errMsg == "requestPayment:ok"){
						  that.apipost("POST","/api/pay/get_order_state",{'order_no':res.data.order_no}).then(req=>{
							  uni.showToast({
								icon :'none',
								title: '支付成功',
								duration: 2000
							  });
							 
							 uni.navigateTo({
							    url: `/pages/money/paysuccess?page=${that.data.page}`
							 });
						  })
					  }
					},
					fail (res) {
						console.log(res,'支付失败');
					}
				})
			},
			//选择支付方式
			changePayType(item) {
				
				this.payType = item.type;
			},
			//确认支付
			clickzhifu(){
				
				let that=this
				if(that.payType==5){
					that.payzhifuquan()
					return
				}
				if(that.payType==3){
					that.qiyetongzhifu()
				}else{
					that.weixinzhifu()
				}
				
			},
			confirm:  function(id) {
				let that=this
				
				if(this.data.type==1){
					that.apiget(`/api/order/get_stock_pay_sign?id=${that.data.id}&amount=${that.data.amount}`).then(res => {
						if(res.Code==10200){
							that.paychaxun(res.Data)
							
						}
						
					}).catch(err=>{
						console.log(err)
					});
				}else{
					that.apiget(`/api/order/get_sign_info?id=${id}`).then(res => {
						if(res.Code==10200){
							that.paychaxun(res.Data)
							
						}
						
					}).catch(err=>{
						console.log(err)
					});
				}
				
			},
		}
	}
	//qiyezhifu
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}
	.icon-qiyezhifu{
		background:url(/static/qiyezhifu.png) no-repeat  0 center;
		background-size:50rpx auto;
	}
	.icon-rengongtongdao{
		background:url(/static/rengongtongdao.png) no-repeat  0 center;
		background-size:60rpx auto;
	}
	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price{
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;
			&:before{
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;
		
		.type-item{
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position:relative;
		}
		
		.icon{
			width: 100upx;
			font-size: 52upx;
		}
		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}
		.icon-weixinzhifu {
			color: #36cb59;
		}
		.icon-alipay {
			color: #01aaef;
		}
		.tit{
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}
		.con{
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}
	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

</style>
